﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionorderss;
using MyMES.Read.API.Dtos;

namespace MyMES.Read.API.Application.CommendHandlers.Productionorderss
{
    public class GetWorkProcessMaterCommendHandlers : IRequestHandler<GetWorkProcessMaterCommends, APIResult<List<WorkpromaterDtos>>>
    {
        private readonly IBaseRepository<woekderProess> _workpromaterRepository;
        private readonly IBaseRepository<Productionorders> proRepository;
        private readonly IBaseRepository<Process>  prosRepository;
        private readonly IMapper mapper;

        public GetWorkProcessMaterCommendHandlers(IBaseRepository<woekderProess> workpromaterRepository, IBaseRepository<Productionorders> proRepository, IBaseRepository<Process> prosRepository, IMapper mapper)
        {
            _workpromaterRepository = workpromaterRepository;
            this.proRepository = proRepository;
            this.prosRepository = prosRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取 工序流程--详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<WorkpromaterDtos>>> Handle(GetWorkProcessMaterCommends request, CancellationToken cancellationToken)
        {
            APIResult<List<WorkpromaterDtos>> result = new APIResult<List<WorkpromaterDtos>>();
            result.Code = APIEnum.查询成功;
            result.Msg = "查询成功";
            string sql = $"select a.*,b.ProcessNo,b.ProcessName,b.Processdescris ,c.PlanStartTime,c.PlanEndTime,c.yield ,c.Head,c.Department FROM woekderproess a LEFT JOIN process b on a.ProcessID=b.ProcessID  LEFT JOIN  Productionorders c on a.ProductionordersId=c.ProductionordersId  where a.woekderProessId='{request.woekderProessId}' ";


            var workpro = _workpromaterRepository.SelectAsync(sql).Result;
            var produc =  proRepository.SelectAsync(sql).Result;
            var proc = prosRepository.SelectAsync(sql).Result;

            var workpros = mapper.Map<List<WorkpromaterDtos>>(workpro);
            var producs = mapper.Map<List<WorkpromaterDtos>>(produc);
            var procs = mapper.Map<List<WorkpromaterDtos>>(proc);
      

            foreach (var item in workpros)
            {
                item.ProcessNo = procs.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault().ProcessNo;
                item.ProcessName = procs.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault().ProcessName;
                item.Processdescris = procs.Where(x => x.ProcessID == item.ProcessID).FirstOrDefault().Processdescris;
                item.PlanStartTime = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanStartTime;
                item.PlanEndTime = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().PlanEndTime;
                item.yield = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().yield;
                item.Head = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().Head;
                item.Department = produc.Where(x => x.ProductionordersId == item.ProductionordersId).FirstOrDefault().Department;


            }

            result.data = workpros;
            return result;
        }
    }
}
